﻿using DevelopTool.Common;
using Sunny.UI;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DevelopTool.Bll.DesktopClockBlls
{
    /// <summary>
    /// 时钟窗体
    /// </summary>
    public partial class ClockForm : Form
    {
        /// <summary>
        /// 所属显示器id
        /// </summary>
        public string ScreenId { get; set; }

        /// <summary>
        /// 显示器对象
        /// </summary>
        private Screen screen;

        /// <summary>
        /// 时间控件
        /// </summary>
        public UISmoothLabel LblTime
        {
            get
            {
                return this.lblTime;
            }
        }

        public ClockForm(Screen screen)
        {
            this.screen = screen;
            InitializeComponent();
        }

        private void ClockForm_Load(object sender, EventArgs e)
        {
            this.lblTime.Font = new System.Drawing.Font(Setting.DesktopClock.FontFamily, Setting.DesktopClock.FontSize, (FontStyle)Enum.Parse(typeof(FontStyle), Setting.DesktopClock.FontStyle, false));
            this.lblTime.RectColor = Setting.DesktopClock.ForeColor;
            
            this.lblTime.Text = DateTime.Now.ToString("HH:mm:ss");

            Bounds = this.screen.Bounds;
            TransparencyKey = BackColor;
            //将窗口置底
            SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            string text = string.Empty;
            if (Setting.DesktopClock.ShowDate)
            {
                text = "     " + DateTime.Now.ToString("HH:mm:ss");
                text += Environment.NewLine + "  "+DateTime.Now.ToString("yyyy年M月d日");
            }
            else
            {
                text = DateTime.Now.ToString("HH:mm:ss");
            }

            this.lblTime.Text = text;
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        public const uint SWP_NOMOVE = 0x2;
        public const uint SWP_NOSIZE = 0x1;
        public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
    }
}
